#include "estadisticadelegate.h"

EstadisticaDelegate::EstadisticaDelegate( QObject* parent ) : QStyledItemDelegate( parent ) {}

void EstadisticaDelegate::paintTitle( QPainter* painter, const QRect& textrect, quint32 separation ) const
{
    painter->save();
    QFont font;
    font.setBold( true );
    painter->setFont( font );
    QRect titlerect;
    painter->drawText( textrect,
                       Qt::AlignLeft | Qt::AlignTop,
                       trUtf8( "Estadísticas" ),
                       &titlerect );
    painter->drawLine( textrect.left(), titlerect.bottom()+separation, textrect.right(), titlerect.bottom()+separation );
    painter->restore();
}

void EstadisticaDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
    quint32 margin = 5;
    quint32 separation = 3;

    QRect textrect = option.rect.marginsRemoved( QMargins( margin, margin, margin, margin ) );
    QRect estadisticarect;
    QRect valorrect;

    if( index.row() == 0 ) return paintTitle( painter, textrect, separation );

    if( option.state & QStyle::State_MouseOver )
    {
        painter->fillRect( option.rect, "#eeeeee" );
        painter->setPen( "lightgrey" );
        painter->drawLine( option.rect.topLeft(), option.rect.topRight() );
        painter->drawLine( option.rect.bottomLeft(), option.rect.bottomRight() );
    }
    painter->setPen( Qt::black );

    painter->drawText( textrect,
                       Qt::AlignLeft | Qt::AlignTop,
                       index.data( EstadisticasModel::EstadisticaRole ).toString(),
                       &estadisticarect );
    painter->drawText( QRect( estadisticarect.left(), (estadisticarect.bottom()+separation), textrect.width(), (textrect.height()-estadisticarect.height()-separation) ),
                       Qt::AlignRight | Qt::AlignTop,
                       index.data( EstadisticasModel::ValorRole ).toString(),
                       &valorrect );

    Inmersion* inmersion = index.data( EstadisticasModel::InmersionRole ).value<Inmersion*>();
    if( inmersion )
    {
        painter->save();
        QFont font;
        font.setPointSize(7);
        painter->setFont( font );
        painter->setPen( "grey" );
        painter->drawText( QRect( estadisticarect.left(), (estadisticarect.bottom()+separation), (textrect.width()-valorrect.width()), (textrect.height()-estadisticarect.height()-separation) ),
                           Qt::AlignLeft | Qt::AlignVCenter,
                           QString( "(%1)" ).arg( inmersion->sitio()->nombre() ) );
        painter->restore();
    }

    if( !(option.state & QStyle::State_MouseOver) )
    {
        painter->setPen( "lightgrey" );
        painter->drawLine( (estadisticarect.right()+separation), estadisticarect.bottom(), textrect.right(), estadisticarect.bottom() );
    }
}
